package pers.lx.crm.service.info.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.lx.crm.base.common.pigeon.Result;
import pers.lx.crm.base.constant.ErrorConstant;
import pers.lx.crm.base.constant.KeyConstants;
import pers.lx.crm.base.util.ResultUtil;
import pers.lx.crm.dao.info.TagDao;
import pers.lx.crm.entiy.info.TagDO;
import pers.lx.crm.service.info.TagService;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by lxl on 2017/2/15.
 */
@Service
public class TagServiceImpl implements TagService{

    @Autowired
    private TagDao tagDao ;

    @Override
    public Result getTags(TagDO tagDO) {
        Result result = new Result() ;
        try {
            tagDO.setList(tagDao.selectTagListPage(tagDO));
            return ResultUtil.getPageResult(tagDO,result);
        } catch (NullPointerException e) {
            e.printStackTrace();
            result.setError(ErrorConstant.PARAMETER_NULL);
        } catch (Exception e){
            e.printStackTrace();
            result.setError(ErrorConstant.UNKNOWN_ERROR);
        }
        return result ;
    }

    @Override
    public Result getTagKinds() {
        Result result = new Result() ;
        try {
            result.addParam(KeyConstants.LIST_KEY,tagDao.selectTagKindList());
        } catch (Exception e){
            e.printStackTrace();
            result.setError(ErrorConstant.UNKNOWN_ERROR);
        }
        return result;
    }

    @Override
    public Result addTag(TagDO tagDO) {
        Result result = new Result() ;
        try {
            tagDao.insertTag(tagDO);
        } catch (Exception e){
            e.printStackTrace();
            result.setError(ErrorConstant.UNKNOWN_ERROR);
        }
        return result;
    }

    @Override
    public Result updateTag(TagDO tagDO) {
        Result result = new Result() ;
        try {
            tagDao.updateTag(tagDO);
        } catch (Exception e){
            e.printStackTrace();
            result.setError(ErrorConstant.UNKNOWN_ERROR);
        }
        return result;
    }

    @Transactional
    @Override
    public Result removeTags(List<TagDO> tagDOs) {
        Result result = new Result() ;
        for (int i = 0 ; i < tagDOs.size() ; i++){
            tagDao.deleteTag(tagDOs.get(i));
        }
        return result;
    }
}
